﻿using System;
using System.Collections.Generic;
using chatGUI.DataBase.Entity;
using chatGUI.DataBase.Helper;
using chatGUI.Model;
using chatGUI.SingeltonData;
using HandyControl.Data;

namespace chatGUI.DataBase.Dao;

public class InfoDao{
        private static readonly IFreeSql  fsql= SqlHelper.FSql;

        /// <summary>
        /// 没有插入 有就更新
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static  Information Save(Information msg){
            return fsql.GetRepository<Information>().InsertOrUpdate(msg);
        }

        public static Information SelectById(int id){
            return fsql.Select<Information>().Where(m => m.Mno == id).First();
        }
        
        /// <summary>
        /// 查询一个用户的所有消息
        /// </summary>
        /// <param name="owner"></param>
        /// <returns></returns>
        public static List<Information> SelectAll(string owner){
            return fsql.Select<Information>().Where(i=>i.Owner == owner).ToList();
        }


        public static int InsertList(List<Information> infos){
            try {
                return fsql.Insert(infos).ExecuteAffrows();
            }
            catch (Exception) {
                return 0;
            }
        }

        /// <summary>
        /// 查询uid账户的两个用户互相通信的最后一条消息
        /// </summary>
        /// <param name="uid">用户</param>
        /// <param name="fid">可以是用户或者群</param>
        /// <returns></returns>
        public static Information SelectLastChat(string uid,string fid){
            if (fid.Contains("u")) {
                return fsql.Select<Information>()
                    .Where(i=>i.Owner == uid && ((i.Sender == uid && i.Receiver == fid) || (i.Sender == fid && i.Receiver == uid)))
                    .OrderByDescending(i=>i.Mno)
                    .First(); 
            }

            if (fid.Contains("g")) {
                return fsql.Select<Information>()
                    .Where(i=>i.Owner == uid && i.Receiver == fid)
                    .OrderByDescending(i=>i.Mno)
                    .First();
            }
            return null;
        }
        
        
        /// <summary>
        /// 查询uid账户两个好友之前的所有消息
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="fid">可以是群或者好友</param>
        /// <returns>删除的记录条数</returns>
        public static List<Information> SelectByUser(string uid, string fid){
            if (fid.Contains("u")) {
                return fsql.Select<Information>()
                    .Where(i=>i.Owner == uid && ((i.Sender == uid && i.Receiver == fid) || (i.Sender == fid && i.Receiver == uid)))
                    .ToList();
            }

            if (fid.Contains("g")) {
                return fsql.Select<Information>()
                    .Where(i=>i.Owner == uid && i.Receiver == fid)
                    .ToList();
            }

            return new List<Information>(0);
        }


        /// <summary>
        /// 查询uid账户两个用户互相通信的所有消息, 或者用户某个群的所有消息
        /// </summary>
        /// <param name="uid">查询者id</param>
        /// <param name="fid">要查询的用户或者群id</param>
        /// <returns></returns>
        public static List<chatBubbleModel> SelectChatModel(string uid,string fid){
            var chats = new List<chatBubbleModel>();
            var infos = SelectByUser(uid,fid);
            foreach (var info in infos) {
                var sender = fsql.Select<FriendList>().Where(f=>f.Uid==uid && f.Fid == fid).ToOne();
                if (sender != null) {
                var chat = new chatBubbleModel {
                    SenderNickName = sender.NickName, SenderAvatarUrlStr = sender.AvatarUrl,
                    SendTime = DateTime.Parse(info.MDate), Message = info.Message, ChatBubbleRole = sender.Uid == info.Owner ? chatBubbleRole.reciever : chatBubbleRole.sender,
                    ChatBubbleType = info.DataType == "text"? chatBubbleType.text: info.DataType == "picture" ? chatBubbleType.picture :info.DataType == "file" ? chatBubbleType.file : chatBubbleType.gif
                    };
                    chats.Add(chat);
                }
            }
            return chats;
        }
    

        /// <summary>
        /// 查询uid账户的一个群的所有消息
        /// </summary>
        /// <param name="gid"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public static List<Information> SelectAllByGroup(string uid,string gid){
            return fsql.Select<Information>()
                .Where(i=>i.Owner == uid && i.Receiver == gid).ToList();
        }

        /// <summary>
        /// 删除uid账户两个好友或群之前的所有消息
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="fid">可以是群或者好友</param>
        /// <returns>删除的记录条数</returns>
        public static int DeleteByUser(string uid, string fid){
            if (fid.Contains("u")) {
                return fsql.Select<Information>()
                    .Where(i=>i.Owner == uid && ((i.Sender == uid && i.Receiver == fid) || (i.Sender == fid && i.Receiver == uid)))
                    .ToDelete().ExecuteAffrows();
            }

            if (fid.Contains("g")) {
                return fsql.Select<Information>()
                    .Where(i=>i.Owner== uid && i.Receiver == fid)
                    .ToDelete().ExecuteAffrows();
            }

            return 0;
        }

        /// <summary>
        /// 通过id删除某条消息
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public static int DeleteById(int no){
            
            try {
                return fsql.Select<Information>().Where(u => u.Mno== no).ToDelete().ExecuteAffrows();
            }
            catch (Exception) {
                return 0;
            }
        }
        
}